package com.monitor.task.util;

import com.monitor.common.DateUtil;
import com.monitor.pojo.MonitorInfo;
import com.monitor.task.email.EmailTemplate;
import com.monitor.task.emunration.EnumEmailWarnMsg;
import com.monitor.vo.enumeration.EnumMonitorType;
import com.monitor.vo.enumeration.EnumQueryType;
import com.monitor.vo.enumeration.MonitorRetStatus;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * @ClassName MonitorTaskUtils
 * @Package com.monitor.task.util
 * @Description 定时任务工具类
 * @Author zhixingbai
 * @Create 2018/10/1713:35
 */
public class MonitorTaskUtils {

    private static final Logger logger = LoggerFactory.getLogger(MonitorTaskUtils.class);

    /**
     * 设置报警规则
     *
     * @param monitorInfo
     */
    public static boolean warning(MonitorInfo monitorInfo, StringBuffer mailContent) {
        //step1 如果retTime为空,对比当前时间-报警间隔时间是否大于等于请求时间
        logger.info(monitorInfo.toString());
        StringBuilder faile = new StringBuilder();
        long startTime = System.currentTimeMillis();
        boolean sendFlag = true;
        try {
            if (StringUtils.isEmpty(monitorInfo.getRetTime())) {
                boolean flag = DateUtil.dateDiff(DateUtil.parse(monitorInfo.getReqTime()), new Date(), monitorInfo.getTimeoutTime());
                if (flag) {
                    //报警发送邮件
                    logger.info(EnumEmailWarnMsg.CHAOSHI.getMsg() + ":{}", monitorInfo.getAccount());
                    faile.append(EnumEmailWarnMsg.CHAOSHI.getMsg() + " | ");
                    sendFlag = false;
                }
            } else {
                //step2 如果retTime不为空,并且状态为020则报警
                if (MonitorRetStatus.FAILE.getCode().equals(monitorInfo.getRetStatus())) {
                    //报警发送邮件
                    sendFlag = false;
                    String faileMsg = EnumEmailWarnMsg.ZHUANGTAI.getMsg() + ":" + monitorInfo.getRetStatus() + "|" + monitorInfo.getRetMsg();
                    logger.info(faileMsg + ",账号:{}", monitorInfo.getAccount());
                    faile.append(faileMsg + " | ");
                    sendFlag = false;
                }
                //step3 如果retTime不为空,对比返回时间-请求时间是否大于等于报警间隔时间
                boolean flag = DateUtil.dateDiff(DateUtil.parse(monitorInfo.getReqTime()), DateUtil.parse(monitorInfo.getRetTime()), monitorInfo.getCostTime());
                if (flag) {
                    //报警发送邮件
                    sendFlag = false;
                    logger.info(EnumEmailWarnMsg.HAOSHI.getMsg() + ",账号:{}", monitorInfo.getAccount());
                    faile.append(EnumEmailWarnMsg.HAOSHI.getMsg() + " | ");
                }

                //step3 如果retTime不为空,对比当前时间-返回时间是否大于等于报警间隔时间
                flag = DateUtil.dateDiff(DateUtil.parse(monitorInfo.getRetTime()), new Date(), monitorInfo.getIntervalTime());
                if (flag) {
                    //报警发送邮件
                    sendFlag = false;
                    logger.info(EnumEmailWarnMsg.TWO_QUERY.getMsg() + ",账号:{}", monitorInfo.getAccount());
                    faile.append(EnumEmailWarnMsg.TWO_QUERY.getMsg());
                }
            }
            if (!sendFlag) {
                mailContent.append(String.format(EmailTemplate.TEMPLATE_BODY,
                        monitorInfo.getBizId(), monitorInfo.getAccount(),
                        EnumMonitorType.getMsgByCode(monitorInfo.getType()),
                        monitorInfo.getBankName(),
                        monitorInfo.getReqTime(), monitorInfo.getRetTime(),
                        monitorInfo.getCostTime(),monitorInfo.getTimeoutTime(),
                        monitorInfo.getIntervalTime(), faile.toString()));
            }
        } catch (Exception e) {
            logger.debug("时间对比规则异常：{}", e.getMessage());
            e.printStackTrace();
        }
        logger.info("时间对比规则，单笔耗时：{}", System.currentTimeMillis() - startTime);
        return sendFlag;
    }

    public static void main(String[] args) {
        System.out.println(EnumMonitorType.getMsgByCode("010"));
    }
}
